# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def sortList(self, head: ListNode) -> ListNode:
        li = []
        while head is not None:
            li.append(head.val)
            head = head.next
        if len(li) == 0:
            return head
        li.sort()
        hh = ListNode(val=li[0])
        te = hh
        for i in range(1,len(li)):
            node = ListNode(val=li[i])
            te.next = node
            te = te.next
        return hh
